﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using StudentCouncil.API.Model;
using StudentCouncil.BLL.Mgr;
using StudentCouncil.Model;
using StudentCouncil.Model.Models;
using StudentCouncil.Model.Parameter;

namespace StudentCouncil.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MainController : ControllerBase
    {
        [HttpPost]
        /// <summary>
        /// 比赛活动的增删改除
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public Pager<Competition> GetCompetitionListByPage(CompetitionParameter parameter)
        {
            Pager<Competition> result = CouncilMgr.GetCompetitionListByPage(parameter);
            return result;
        }
        public ResultDTO DeleteById(int id)
        {
            bool success = CouncilMgr.DeleteById(id);
            ResultDTO state = new ResultDTO(success);
            return state;
        }
        public ResultDTO BatchDelete(string ids)
        {
            ResultDTO state = new ResultDTO();
            state.Status = CouncilMgr.BatchDelete(ids);
            return state;
        }
        [HttpPost]
        public ResultDTO SaveCompetition(Competition entity)
        {
            bool success = CouncilMgr.AddCompetition(entity);
            ResultDTO state = new ResultDTO(success);
            if (!state.Status)
            {
                state.Message = "新增发生异常，请重新添加！";
            }
            return state;
        }
        public Competition getCompetitionById(int id)
        {
            //var dto = new ResultDTO();
            Competition entity = CouncilMgr.GetCompetitionById(id);
            //dto.Data = entity;
            return entity;
        }
        public ResultDTO CheckCompetitionById(int id)
        {
            var db = new ResultDTO();
            bool success = CouncilMgr.CheckCompetitionById(id);
            db.Status = success;
            return db;
        }
        public ResultDTO UpdateCompetition(Competition entity)
        {
            bool success = CouncilMgr.Update(entity);
            ResultDTO state = new ResultDTO(success);
            if (!state.Status)
            {
                state.Message = "更新失败，请重新更新！";
            }
            return state;
        }

        /// <summary>
        /// 比赛活动状态申请表的增删改
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        [HttpPost]
        public Pager<Competitionstatus> GetCompetitionstatusListByPage(CompetitionstatusParameter parameter)
        {
            Pager<Competitionstatus> result = CouncilMgr.GetCompetitionstatusListByPage(parameter);
            return result;
        }
        [HttpPost]
        public ResultDTO Updatestatus(Competitionstatus entity)
        {
            var dto = new ResultDTO();

            if (null == entity)
            {
                return dto;
            }
            var res = CouncilMgr.Updatestatus(entity);
            dto.Data = res;
            dto.Status = res;
            return dto;
        }
        [HttpPost]
        public ResultDTO SaveCompetitionStatus(Competitionstatus entity)
        {
            bool success = CouncilMgr.AddCompetitionStatus(entity);
            ResultDTO state = new ResultDTO(success);
            if (!state.Status)
            {
                state.Message = "新增发生异常，请重新添加！";
            }
            return state;
        }

        /// <summary>
        /// 上传图片文件业务
        /// </summary>
        /// <param name="file"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultDTO Upload(IFormFile file, int userId)
        {
            JsonResult result1 = new JsonResult(new { name = "ok" });
            JsonResult result2 = new JsonResult(new { name = "no" });
            if (file != null)
            {
                var fileDir = "D:\\阿涛svn\\Git\\Web_StudentCouncil\\studentsale_app\\Companysale_app\\static\\UploadUserImg";
                if (!Directory.Exists(fileDir))
                {
                    Directory.CreateDirectory(fileDir);
                }
                //文件名称
                string projectFileName = file.FileName;

                //上传的文件的路径
                string filePath = fileDir + $@"\{projectFileName}";
                using (FileStream fs = System.IO.File.Create(filePath))
                {
                    file.CopyTo(fs);
                    fs.Flush();
                }
                User entity = new User();
                entity.HeadImg = filePath;
                entity.UserId = userId;
                bool res = UserMgr.UpdateImage(entity);
                ResultDTO state = new ResultDTO(res);
                return state;
            }
            else
            {
                return null;
            }
        }

        [HttpPost]
        public JsonResult UploadST(IFormFile file, string userId)
        {
            JsonResult result1 = new JsonResult(new { name = "ok" });
            JsonResult result2 = new JsonResult(new { name = "no" });
            if (file != null)
            {
                var fileDir = "D:\\照片\\个人信息文件";
                if (!Directory.Exists(fileDir))
                {
                    Directory.CreateDirectory(fileDir);
                }
                //文件名称
                string projectFileName = file.FileName;

                //上传的文件的路径
                string filePath = fileDir + $@"\{projectFileName}";
                using (FileStream fs = System.IO.File.Create(filePath))
                {
                    file.CopyTo(fs);
                    fs.Flush();
                }
                return result1;
            }
            else
            {
                return result2;
            }
        }

        [HttpPost]
        /// <summary>
        /// 社团列表的增删改除
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public Pager<Association> GetAssociationListByPage(AssociationParameter parameter)
        {
            Pager<Association> result = AssociationMgr.GetAssociationListByPage(parameter);
            return result;
        }
        [HttpPost]
        public ResultDTO AssociationDeleteById(int id)
        {
            bool success = AssociationMgr.AssociationDeleteById(id);
            ResultDTO state = new ResultDTO(success);
            return state;
        }
        public ResultDTO AssociationBatchDelete(string ids)
        {
            ResultDTO state = new ResultDTO();
            state.Status = AssociationMgr.BatchDelete(ids);
            return state;
        }
        
        public ResultDTO SaveAssociation(Association entity)
        {
            bool success = AssociationMgr.AddAssociation(entity);
            ResultDTO state = new ResultDTO(success);
            if (!state.Status)
            {
                state.Message = "新增发生异常，请重新添加！";
            }
            return state;
        }
        public Association getAssociationById(int id)
        {
            Association entity = AssociationMgr.GetAssociationById(id);
            return entity;
        }
        public ResultDTO CheckAssociationById(int id)
        {
            bool success = AssociationMgr.CheckAssociationById(id);
            ResultDTO state = new ResultDTO(success);
            return state;
        }
        [HttpPost]
        public ResultDTO UpdateAssociation(Association entity)
        {
            bool success = AssociationMgr.AssociationUpdate(entity);
            ResultDTO state = new ResultDTO(success);
            if (!state.Status)
            {
                state.Message = "更新失败，请重新更新！";
            }
            return state;
        }
       
        [HttpPost]
        /// <summary>
        /// 社团活动状态审核表
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public Pager<Associationstatus> GetAssociationstatusListByPage(AssociationstatusParameter parameter)
        {
            Pager<Associationstatus> result = AssociationMgr.GetAssociationstatusListByPage(parameter);
            return result;
        }

        [HttpGet]
        public ResultDTO Updateassociation(Associationstatus entity)
        {
            var dto = new ResultDTO();

            if (null == entity)
            {
                return dto;
            }
            var res = AssociationMgr.AssociationstatusUpdate(entity);
            dto.Data = res;
            dto.Status = res;
            return dto;
        }
        
        [HttpPost]
        public Associationstatus AssociationStatus(Associationstatus entity)
        {
            var a = AssociationMgr.AddAssociationstatus(entity);
            //ResultDTO state = new ResultDTO(success);
            //if (!a)
            //{
            //    state.Message = "新增发生异常，请重新添加！";
            //}
            return a;
        }

        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="file"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultDTO Uploadwenjian(IFormFile file, int associationId)
        {
            if (file != null)
            {
                var fileDir = "D:\\阿涛svn\\Git\\Web_StudentCouncil\\studentsale_app\\Companysale_app\\static\\Uploadwendong";
                if (!Directory.Exists(fileDir))
                {
                    Directory.CreateDirectory(fileDir);
                }
                //文件名称
                string projectFileName = file.FileName;

                //上传的文件的路径
                string filePath = fileDir + $@"\{projectFileName}";
                using (FileStream fs = System.IO.File.Create(filePath))
                {
                    file.CopyTo(fs);
                    fs.Flush();
                }
                Associationstatus entity = new Associationstatus();
                entity.Wendong = filePath;
                entity.AssociationId = associationId;
                bool res = AssociationMgr.Updatewenjian(entity);
                ResultDTO state = new ResultDTO(res);
                return state;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 上传视频
        /// </summary>
        /// <param name="file"></param>
        /// <param name="associationId"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultDTO UploadVideostatus(IFormFile file, int Id)
        {
            if (file != null)
            {
                var fileDir = "D:\\阿涛svn\\Git\\Web_StudentCouncil\\studentsale_app\\Companysale_app\\static\\UploadVideo";
                if (!Directory.Exists(fileDir))
                {
                    Directory.CreateDirectory(fileDir);
                }
                //文件名称
                string projectFileName = file.FileName;

                //上传的文件的路径
                string filePath = fileDir + $@"\{projectFileName}";
                using (FileStream fs = System.IO.File.Create(filePath))
                {
                    file.CopyTo(fs);
                    fs.Flush();
                }
                Associationstatus entity = new Associationstatus();
                entity.Video = filePath;
                entity.AssociationId = Id;
                bool res = AssociationMgr.UpdateVideostatus(entity);
                ResultDTO state = new ResultDTO(res);
                return state;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 今日疑惑提交
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public ResultDTO UpdateInfo(Message entity)
        {
            bool success = MessageMgr.UpdateInfo(entity);
            ResultDTO state = new ResultDTO(success);
            if (!state.Status)
            {
                state.Message = "提交发生异常，请重新提交！";
            }
            return state;
        }
            public ResultDTO AddMessage(Message entity)
        {
            bool success = MessageMgr.AddMessage(entity);
            ResultDTO state = new ResultDTO(success);
            if (!state.Status)
            {
                state.Message = "提交发生异常，请重新提交！";
            }
            return state;
        }
        


        public ResultDTO YHDeleteById(int id)
        {
            bool success = MessageMgr.YHDeleteById(id);
            ResultDTO state = new ResultDTO(success);
            return state;
        }
        public ResultDTO YHBatchDelete(string ids)
        {
            ResultDTO state = new ResultDTO();
            state.Status = MessageMgr.YHBatchDelete(ids);
            return state;
        }

        [HttpPost]
        /// <summary>
        /// 学员表单的业务
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public Pager<StudentUnion> GetListStudentByPage(StudentUnionParameter parameter)
        {
            Pager<StudentUnion> result = StudentUnionMgr.GetListPage(parameter);
            return result;
        }

        public ResultDTO SaveStudent(StudentUnion entity)
        {
            bool success = StudentUnionMgr.AddStudent_union(entity);
            ResultDTO state = new ResultDTO(success);
            if (!state.Status)
            {
                state.Message = "新增发生异常，请重新添加！";
            }
            return state;
        }
        public ResultDTO DeleteStudentById(int id)
        {
            bool success = StudentUnionMgr.DeleteById(id);
            ResultDTO state = new ResultDTO(success);
            return state;
        }

        public StudentUnion GetStudentById(int id)
        {
            StudentUnion entity = StudentUnionMgr.GetStudentById(id);
            return entity;
        }
        public ResultDTO CheckStudentById(int id)
        {
            bool success = StudentUnionMgr.CheckStudentById(id);
            ResultDTO state = new ResultDTO(success);
            return state;
        }
        public ResultDTO UpdateStudent(StudentUnion entity)
        {
            bool success = StudentUnionMgr.Update(entity);
            ResultDTO state = new ResultDTO(success);
            if (!state.Status)
            {
                state.Message = "更新失败，请重新更新！";
            }
            return state;
        }

        /// <summary>
        /// 任务功能业务
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public List<Renwu> GetRwList() {
            return RenwuMgr.GetList();
        }
        public ResultDTO UpdateRw(Renwu entity) {
            bool success = RenwuMgr.UpdateRw(entity);
            ResultDTO state = new ResultDTO(success);
            if (!state.Status)
            {
                state.Message = "更新失败，请重新更新！";
            }
            return state;
        }
        /// <summary>
        /// 动态表业务功能
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public List<Grade> GetDtList()
        {
            return DongtaiMgr.GetDtList();
        }
        public  ResultDTO AddDongtai(Grade entity)
        {
         
            bool success = DongtaiMgr.AddDongtai(entity);
            ResultDTO state = new ResultDTO(success);
            if (!state.Status)
            {
                state.Message = "新增发生异常，请重新添加！";
            }
            return state;
        }
        /// <summary>
        /// 话题业务的实现
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public Pager<Message> GetMessageListByPage(MessageParameter parameter)
        {
            Pager<Message> result = MessageMgr.GetMessageListByPage(parameter);
            return result;
        }
        [HttpPost]
        public ResultDTO UploadMV(IFormFile file, int Id)
        {
            if (file != null)
            {
                var fileDir = "D:\\阿涛svn\\Git\\Web_StudentCouncil\\studentsale_app\\Companysale_app\\static\\UploadVideo";
                if (!Directory.Exists(fileDir))
                {
                    Directory.CreateDirectory(fileDir);
                }
                //文件名称
                string projectFileName = file.FileName;

                //上传的文件的路径
                string filePath = fileDir + $@"\{projectFileName}";
                using (FileStream fs = System.IO.File.Create(filePath))
                {
                    file.CopyTo(fs);
                    fs.Flush();
                }
                Message entity = new Message();
                entity.MeVideo = filePath;
                entity.MessageId = Id;
                bool res = MessageMgr.UploadMV(entity);
                ResultDTO state = new ResultDTO(res);
                return state;
            }
            else
            {
                return null;
            }
        }

    }
}